<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="项目名称" size="small">
            <el-input v-model="searchForm.projectName"></el-input>
          </el-form-item>

        </el-col>
        <el-col class="singleDatePicker">
          <el-form-item label="日期" size="small">
            <el-date-picker
              v-model="searchForm.drawingDate"
              type="date"
              value-format="timestamp"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()"
                 v-if="roleType===0 || roleType===1 || roleType===2 || roleType===10 || roleType===13 ">添加
      </el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="1300px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="120px">
          <el-row>
            <el-col class="singleDatePicker">
              <el-form-item label="日期" size="small">
                <el-date-picker
                  v-model="teamRow.drawingDate"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="项目类别" size="small">
                <el-input type="text" v-model="teamRow.projectType"></el-input>
              </el-form-item>
              <!--<el-form-item label="图审结束时间" size="small">-->
                <!--<el-date-picker-->
                  <!--v-model="teamRow.reviewEndTime"-->
                  <!--type="date"-->
                  <!--value-format="timestamp"-->
                  <!--placeholder="选择日期"-->
                  <!--style="width: 170px"-->
                <!--&gt;-->
                <!--</el-date-picker>-->
              <!--</el-form-item>-->
              <el-form-item label="合格图纸时间" size="small">
                <el-date-picker
                  v-model="teamRow.qualifiedDrawingsDate"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item label="联系人" size="small">
                <el-input type="text" v-model="teamRow.contact"></el-input>
              </el-form-item>
              <el-form-item label="规模" size="small">
                <el-input type="text" v-model="teamRow.scale"></el-input>
              </el-form-item>
              <el-form-item label="合格图纸" size="small">
                <input type="file" ref='qualifiedDrawings' @change="fileUpload($event,'qualifiedDrawings')">
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item label="所属地" size="small">
                <el-input type="text" v-model="teamRow.affiliation"></el-input>
              </el-form-item>
              <el-form-item label="合格审查书时间" size="small">
                <el-date-picker
                  v-model="teamRow.reviewDate"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item label="项目名称" size="small">
                <el-input type="text" v-model="teamRow.projectName"></el-input>
              </el-form-item>
              <el-form-item label="合格审查书" size="small">
                <input type="file" ref='review' @change="fileUpload($event,'review')">
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col class="singleDatePicker">
              <el-form-item label="一审" size="small">
                <input type="file" ref='firstInstance' @change="fileUpload($event,'firstInstance')">
              </el-form-item>
              <el-form-item label="二审" size="small">
                <input type="file" ref='secondInstance' @change="fileUpload($event,'secondInstance')">
              </el-form-item>
              <el-form-item label="三审" size="small">
                <input type="file" ref='thirdInstance' @change="fileUpload($event,'thirdInstance')">
              </el-form-item>
              <el-form-item label="四审" size="small">
                <input type="file" ref='fourInstance' @change="fileUpload($event,'fourInstance')">
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item label="一审时间" size="small">
                <el-date-picker
                  v-model="teamRow.firstInstanceDate"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="二审时间" size="small">
                <el-date-picker
                  v-model="teamRow.secondInstanceDate"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="三审时间" size="small">
                <el-date-picker
                  v-model="teamRow.thirdInstanceDate"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="四审时间" size="small">
                <el-date-picker
                  v-model="teamRow.fourInstanceDate"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item label="一审意见" size="small">
                <input type="file" ref='firstInstanceIdea' @change="fileUpload($event,'firstInstanceIdea')">
              </el-form-item>
              <el-form-item label="二审意见" size="small">
                <input type="file" ref='secondInstanceIdea' @change="fileUpload($event,'secondInstanceIdea')">
              </el-form-item>
              <el-form-item label="三审意见" size="small">
                <input type="file" ref='thirdInstanceIdea' @change="fileUpload($event,'thirdInstanceIdea')">
              </el-form-item>
              <el-form-item label="四审意见" size="small">
                <input type="file" ref='fourInstanceIdea' @change="fileUpload($event,'fourInstanceIdea')">
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item label="一审意见时间" size="small">
                <el-date-picker
                  v-model="teamRow.firstIdeaDate"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="二审意见时间" size="small">
                <el-date-picker
                  v-model="teamRow.secondIdeaTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="三审意见时间" size="small">
                <el-date-picker
                  v-model="teamRow.thirdIdeaTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="四审意见时间" size="small">
                <el-date-picker
                  v-model="teamRow.fourIdeaTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 170px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small" v-if="isSubmit">文件上传中。。。{{precent}}%</el-button>
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click v-if="!isSubmit">确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="100"
        v-if="roleType===0 || roleType===1 || roleType===2 || roleType===10 || roleType===13 "
      >
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.drawingId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    getList,
    addGraphReviewData,
    selectGraphReviewData,
    deleteGraphReviewData,
    updateGraphReviewData
  } from '@/api/chartreview'
  import myTable from '@/components/myTable'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const time = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const filePath = {
    props: ['colConfig'],
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        <div>
         <a v-if="scope.row[colConfig.prop]" target="_blank" :href="GLOBAL.filePathUrl+ encodeURI(scope.row[colConfig.prop])">下载</a>
         <a v-else style="color: red">X</a>
        </div>
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'drawingDate', label: '日期', width: '100', component: time },
          { prop: 'contact', label: '联系人', width: '100' },
          { prop: 'affiliation', label: '所属地', width: '100' },
          { prop: 'projectName', label: '项目名称', width: '100' },
          { prop: 'projectType', label: '项目类别', width: '100' },
          { prop: 'scale', label: '规模', width: '100' },
          { prop: 'reviewDate', label: '合格审查书时间', width: '130', component: time },
          { prop: 'reviewPath', label: '合格审查书', width: '100', component: filePath },
          // { prop: 'reviewEndTime', label: '图审结束时间', width: '120', component: time },
          { prop: 'qualifiedDrawingsPath', label: '合格图纸', width: '100', component: filePath },
          { prop: 'qualifiedDrawingsDate', label: '合格图纸时间', width: '120', component: time },
          { prop: 'firstInstancePath', label: '一审', width: '100', component: filePath },
          { prop: 'firstInstanceDate', label: '一审时间', width: '100', component: time },
          { prop: 'firstInstanceIdeaPath', label: '一审意见', width: '100', component: filePath },
          { prop: 'firstIdeaDate', label: '一审意见时间', width: '120', component: time },
          { prop: 'secondInstancePath', label: '二审', width: '100', component: filePath },
          { prop: 'secondInstanceDate', label: '二审时间', width: '100', component: time },
          { prop: 'secondInstanceIdeaPath', label: '二审意见', width: '100', component: filePath },
          { prop: 'secondIdeaTime', label: '二审意见时间', width: '120', component: time },
          { prop: 'thirdInstancePath', label: '三审', width: '100', component: filePath },
          { prop: 'thirdInstanceDate', label: '三审时间', width: '100', component: time },
          { prop: 'thirdInstanceIdeaPath', label: '三审意见', width: '100', component: filePath },
          { prop: 'thirdIdeaTime', label: '三审意见时间', width: '120', component: time },
          { prop: 'fourInstancePath', label: '四审', width: '100', component: filePath },
          { prop: 'fourInstanceDate', label: '四审时间', width: '100', component: time },
          { prop: 'fourInstanceIdeaPath', label: '四审意见', width: '100', component: filePath },
          { prop: 'fourIdeaTime', label: '四审意见时间', width: '120', component: time },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        costTypes: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增图审数据',
        marginTitle: '',
        marginType: 1,
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        margin: {},
        marginClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        marginVisible: false,
        pca: pca,
        rules: {
          time: [
            { required: true, message: '请选择日期', trigger: 'change' }
          ],
          projectNo: [
            { required: true, message: '请输入工程号', trigger: 'change' }
          ],
          projectName: [
            { required: true, message: '请输入项目名称', trigger: 'change' }
          ]
        },
        visible: false,
        isSubmit: false, // 增加修改时判断文件是否上传完成
        precent: ''
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      roleType() {
        return this.$store.state.user.roleType
      },
      teamId() {
        return this.$store.state.user.teamId
      }
    },
    methods: {
      upLoad(upLoad, params, callback1, callback2) {
        upLoad(params, function(progressEvent) { // 原生获取上传进度的事件
          if (progressEvent.lengthComputable) {
            // 属性lengthComputable主要表明总共需要完成的工作量和已经完成的工作是否可以被测量
            // 如果lengthComputable为false，就获取不到progressEvent.total和progressEvent.loaded
            callback1(progressEvent)
          }
        }).then(res => {
          callback2(res.data)
        }).catch(() => {
          this.outerVisible = false
          this.isSubmit = false
          this.progress = ''
        })
      },
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          console.log(this.tableData)
          this.listLoading = false
        })
      },
      provinceChange() {
        this.searchForm.cityId = ''
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      fileClear() {
        if (this.$refs.review) {
          this.$refs.review.value = ''
        }
        if (this.$refs.qualifiedDrawings) {
          this.$refs.qualifiedDrawings.value = ''
        }
        if (this.$refs.firstInstance) {
          this.$refs.firstInstance.value = ''
        }
        if (this.$refs.firstInstanceIdea) {
          this.$refs.firstInstanceIdea.value = ''
        }
        if (this.$refs.secondInstance) {
          this.$refs.secondInstance.value = ''
        }
        if (this.$refs.secondInstanceIdea) {
          this.$refs.secondInstanceIdea.value = ''
        }
        if (this.$refs.thirdInstance) {
          this.$refs.thirdInstance.value = ''
        }
        if (this.$refs.thirdInstanceIdea) {
          this.$refs.thirdInstanceIdea.value = ''
        }
        if (this.$refs.fourInstance) {
          this.$refs.fourInstance.value = ''
        }
        if (this.$refs.fourInstanceIdea) {
          this.$refs.fourInstanceIdea.value = ''
        }
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增图审数据'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 1
        this.$set(this.teamRow, 'teamId', this.teamId)
        this.fileClear()
      },
      editTeam(drawingId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改图审数据'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          drawingId: drawingId
        }
        selectGraphReviewData(params).then(response => {
          this.teamRow = response.responseBody
        })
        this.fileClear()
      },
      submitForm(formName) {
        const params = new FormData()
        if (this.teamRow.drawingDate) {
          params.append('drawingDate', this.teamRow.drawingDate)
        }
        params.append('teamId', this.teamRow.teamId)
        params.append('contact', this.teamRow.contact || '')
        params.append('affiliation', this.teamRow.affiliation || '')
        params.append('projectName', this.teamRow.projectName || '')
        params.append('projectType', this.teamRow.projectType || '')
        params.append('scale', this.teamRow.scale || '')
        if (this.teamRow.reviewDate) {
          params.append('reviewDate', this.teamRow.reviewDate)
        }
        if (this.teamRow.review) {
          params.append('review', this.teamRow.review)
        }
        if (this.teamRow.reviewEndTime) {
          params.append('reviewEndTime', this.teamRow.reviewEndTime)
        }
        if (this.teamRow.qualifiedDrawingsDate) {
          params.append('qualifiedDrawingsDate', this.teamRow.qualifiedDrawingsDate)
        }
        if (this.teamRow.qualifiedDrawings) {
          params.append('qualifiedDrawings', this.teamRow.qualifiedDrawings)
        }
        if (this.teamRow.firstInstance) {
          params.append('firstInstance', this.teamRow.firstInstance)
        }
        if (this.teamRow.firstInstanceDate) {
          params.append('firstInstanceDate', this.teamRow.firstInstanceDate)
        }
        if (this.teamRow.firstInstanceIdea) {
          params.append('firstInstanceIdea', this.teamRow.firstInstanceIdea)
        }
        if (this.teamRow.firstIdeaDate) {
          params.append('firstIdeaDate', this.teamRow.firstIdeaDate)
        }
        if (this.teamRow.secondInstance) {
          params.append('secondInstance', this.teamRow.secondInstance)
        }
        if (this.teamRow.secondInstanceDate) {
          params.append('secondInstanceDate', this.teamRow.secondInstanceDate)
        }
        if (this.teamRow.secondInstanceIdea) {
          params.append('secondInstanceIdea', this.teamRow.secondInstanceIdea)
        }
        if (this.teamRow.secondIdeaTime) {
          params.append('secondIdeaTime', this.teamRow.secondIdeaTime)
        }
        if (this.teamRow.thirdInstance) {
          params.append('thirdInstance', this.teamRow.thirdInstance)
        }
        if (this.teamRow.thirdInstanceDate) {
          params.append('thirdInstanceDate', this.teamRow.thirdInstanceDate)
        }
        if (this.teamRow.thirdInstanceIdea) {
          params.append('thirdInstanceIdea', this.teamRow.thirdInstanceIdea)
        }
        if (this.teamRow.thirdIdeaTime) {
          params.append('thirdIdeaTime', this.teamRow.thirdIdeaTime)
        }
        if (this.teamRow.fourInstance) {
          params.append('fourInstance', this.teamRow.fourInstance)
        }
        if (this.teamRow.fourInstanceDate) {
          params.append('fourInstanceDate', this.teamRow.fourInstanceDate)
        }
        if (this.teamRow.fourInstanceIdea) {
          params.append('fourInstanceIdea', this.teamRow.fourInstanceIdea)
        }
        if (this.teamRow.fourIdeaTime) {
          params.append('fourIdeaTime', this.teamRow.fourIdeaTime)
        }
        if (!this.dialogType) {
          params.append('drawingId', this.teamRow.drawingId)
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              this.upLoad(addGraphReviewData, params, (res) => {
                const loaded = res.loaded
                const total = res.total
                this.$nextTick(() => {
                  this.isSubmit = true
                  this.precent = (loaded / total) * 100
                })
              }, (res) => {
                this.outerVisible = false
                this.isSubmit = false
                this.progress = ''
                this.fetchData()
              })
            } else {
              this.upLoad(updateGraphReviewData, params, (res) => {
                const loaded = res.loaded
                const total = res.total
                this.$nextTick(() => {
                  this.isSubmit = true
                  this.precent = (loaded / total) * 100
                })
              }, (res) => {
                this.outerVisible = false
                this.isSubmit = false
                this.progress = ''
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          drawingId: scope.row.drawingId,
          isDel: 1
        }
        deleteGraphReviewData(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      fileUpload(event, teamRowName) {
        this.$set(this.teamRow, teamRowName, event.target.files[0])
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  a, a:focus, a:hover {
    color: #409EFF;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
